layui.use(['table','util','form', 'layer', 'jquery'], function(){
    /*********************************** 首页 ***************************************/
    var table = layui.table, util=layui.util;
    var $ = layui.jquery;
    var form = layui.form,layer = layui.layer;

    //第一个实例
    table.render({
        id          : 'projectReload',
        elem        : '#projectList',
        url         : localStorage.getItem("url")+'code-generator/project/query', //数据接口
        method      : 'POST',
        request     : {pageName : 'pageNo',limitName: 'pageSize'},
        headers     : {"token":localStorage.getItem("token")},
        page        : true, //开启分页
        loading     : true,
        contentType : 'application/json',
        limit       : 10,
        responseHandler : function (res) {
            // 可进行数据操作
            return {
                "count": res.data.total,
                "data" : res.data.data,
                "code" : res.code == 100000 ? 0 : -1 //code值为200表示成功
            };
        },
        cols: [[ //表头
            {field: 'code', title: 'CODE' ,width:180, sort: true},
            {field: 'cnName', title: '项目名称' ,width:120 , sort: true},
            {field: 'status', title: '状态' ,width:120, sort: true, templet: function(d) {return formatStatus(d.status);}},
            {field: 'createTime', title: '创建时间' ,width:180,  sort: true, templet: function(d) {return util.toDateString(d.createTime);}},
            {field:'right', title: '操作' ,toolbar:"#projectBar"}
        ]]
    });

    var active = {
        reload: function(){
            var projectReload = $('#projectReload');
            //执行重载
            table.reload('projectReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    cnName: projectReload.val()
                }
            });
        }
    };

    $('.projectTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    /************************************* 按钮 *******************************************/
    table.on('tool(projectList)', function(obj) {//监听表格列表每一行
        var data = obj.data;

        if(obj.event === 'edit') {//启用

            x_admin_show('修改','project-add.html?code='+data.code+'&detail=2')

        } else if(obj.event === 'submit') {//停用

            updateStatus('提交', 2, obj, 2, data)

        } else if(obj.event === 'del') {//删除

            updateStatus('删除', 3, obj, 3, data)

        } else if(obj.event === 'detail') {//修改

            x_admin_show('详情','project-add.html?code='+data.code+'&detail=1')

        } else if(obj.event === 'init') {//初始化构建

            updateStatus('初始化构建', 5, obj, 5, data)

        } else if(obj.event === 'append') {//追加构建

            updateStatus('追加构建', 6, obj, 6, data)

        } else if(obj.event === 'db') {//创建数据库

            x_admin_show('详情','db-add.html?code='+data.code+"&pName="+data.cnName)

        } else if(obj.event === 'table') {//添加表结构

            x_admin_show('详情','table-add.html?code='+data.code+"&pName="+data.cnName)

        } else if(obj.event === 'download') {//添加表结构

            var obj = new Object();
            obj.code = data.code;
            myAjaxForMsg($,'code-generator/generator/java/download', obj, function(res){
                alert("复制链接用新窗口打开即可下载：" + res);
            })

        }

    })

    /*********************************** 新增 ***************************************/
        $("#addProject").on('click', function() {
            x_admin_show('添加','project-add.html')
        })

        //监听提交
        form.on('submit(addProject)', function(data){
            myAjax($,'code-generator/project/save', data.field, function(){
                layer.alert("保存成功", {icon: 6},function () {
                    // 获得frame索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                    window.parent.layui.table.reload("projectReload",{});
                });
            })
            return false;
        });
    //监听提交
    form.on('submit(addDb)', function(data){
        myAjax($,'code-generator/db/save', data.field, function(){
            layer.alert("保存成功", {icon: 6},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
                window.parent.layui.table.reload("projectReload",{});
            });
        })
        return false;
    });

    //监听提交
    form.on('submit(addTable)', function(data){
        myAjax($,'code-generator/db/table/save', data.field, function(){
            layer.alert("保存成功", {icon: 6},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
                window.parent.layui.table.reload("projectReload",{});
            });
        })
        return false;
    });

});
function formatStatus(status) {
    if(1 == status) {
        return '草稿';
    }
    if(2 == status) {
        return '已提交';
    }
    if(3 == status) {
        return '创建数据库';
    }
    if(4 == status) {
        return '初始化构建';
    }
    return status;
}
function updateStatus(msg, bol, obj, status, data) {
    var emptyObj = new Object();
    emptyObj.code = data.code;
    var targetUrl = '';
    if(1 == bol) {
        //保存跳页面
    } else if(2 == bol) {
        targetUrl = 'code-generator/project/'+'submit';
    } else if(3 == bol) {
        targetUrl = 'code-generator/project/'+'delete';
    }else if(5 == bol) {
        targetUrl = 'code-generator/generator/java/'+'init';
    } else if(6 == bol) {
        targetUrl = 'code-generator/generator/java/'+'append';
    }
    layer.confirm('您确定要'+msg+'这个项目吗？', function(index){
        myAjax($,targetUrl, emptyObj, function(){
            //修改这一行
            if(3 == bol) {
                obj.del();
            } else {
                $('.projectTable .layui-btn').click()
            }
            //关闭弹框
            layer.close(index);
        })
    });
}